package com.jiuyin.dianjing.ui.activity.team;

import android.content.Intent;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.data.TokenJson;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.ToastUtil;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BadgeGridActivity extends BaseActivity {
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.gv_badge)
    GridView gvBadge;
    List<String> badges = new ArrayList<>();
    private CommonAdapter<String> adapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_badge_grid;
    }

    @Override
    protected void initData() {
        JSONObject data = null;
        try {
            data = new TokenJson();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiResultCallback requestCallback = new DefaultApiResult() {
            @Override
            public void onSuccess(JsonObject data) {
                List<String> teamLogoList = new Gson().fromJson(data.get("teamLogoList"), new TypeToken<List<String>>() {
                }.getType());
                badges.clear();
                badges.addAll(teamLogoList);
                adapter.notifyDataSetChanged();
            }
        };
        ServerApi.post("api/common/getTeamLogoList", data, null, progress, d -> addDisposable(d), requestCallback);
    }

    @Override
    protected void initView() {
        setTitle("选择徽章");
        btSubmit.setOnClickListener(v -> {
            if (gvBadge.getCheckedItemCount() == 0) {
                ToastUtil.showShort("请选择队徽");
                return;
            }
            Intent data = getIntent();
            data.putExtra("selected", badges.get(gvBadge.getCheckedItemPosition()));
            setResult(RESULT_OK, data);
            finish();
        });
        gvBadge.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        gvBadge.setOnItemClickListener((parent, view, position, id) -> {
            adapter.notifyDataSetChanged();
        });
        adapter = new CommonAdapter<String>(this, R.layout.item_badge, badges) {

            @Override
            protected void convert(ViewHolder viewHolder, String item, int position) {
                ImageView ivBadge = viewHolder.getView(R.id.iv_badge);
                Glide.with(BadgeGridActivity.this)
                        .load(badges.get(position))
                        .override(DisplayUtil.dp2px(BadgeGridActivity.this, 53.3f))
                        .into(ivBadge);
                if (gvBadge.isItemChecked(position)) {
                    viewHolder.setBackgroundRes(R.id.iv_badge, R.drawable.item_match_game_select_select_bg);
                } else {
                    viewHolder.setBackgroundRes(R.id.iv_badge, R.drawable.item_match_game_select_normal_bg);
                }
            }
        };
        gvBadge.setAdapter(adapter);
    }
}
